package com.macaline.concurrent;

import java.util.concurrent.*;

public class ThreadPoolDemo {




    public static void main(String[] args) {
        // 一池多线程
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        // 一池1线程
//        ExecutorService executorService = Executors.newSingleThreadExecutor();
////        // 一池可扩容线程
//        ExecutorService executorService = Executors.newCachedThreadPool();

        ExecutorService executorService = new ThreadPoolExecutor(
                3,
                4,
                2L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 20; i++) {
            int finalI = i;
            executorService.execute(()->{

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "处理数据" + finalI);
            });
        }



        executorService.shutdown();
        System.out.println("主线程执行完成");
    }
}
